package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.Result;
import com.wing.enterprise.device.entity.Device;
import com.wing.enterprise.device.response.ProcessDeviceVO;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.productionPlan.dao.SimpleWorkOrderDeviceDao;
import com.wing.productionPlan.model.entity.SimpleWorkOrderDevice;
import com.wing.productionPlan.model.request.SimpleWorkOrderDeviceForm;
import com.wing.productionPlan.model.response.ProductionProcessDeviceVO;
import com.wing.productionPlan.model.response.SimpleWorkOrderDeviceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 简版工单设备表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SimpleWorkOrderDeviceServiceImpl extends ServiceImpl<SimpleWorkOrderDeviceDao, SimpleWorkOrderDevice> implements SimpleWorkOrderDeviceService {
    @Autowired
    private EnterpriseClient enterpriseClient;
    @Override
    public int add(SimpleWorkOrderDeviceForm simpleWorkOrderDeviceForm) {

        //校验

        SimpleWorkOrderDevice simpleWorkOrderDevice =new SimpleWorkOrderDevice();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SimpleWorkOrderDevice.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(simpleWorkOrderDeviceForm, simpleWorkOrderDevice, copyOptions);

        return baseMapper.insert(simpleWorkOrderDevice);
    }

    @Override
    public List<SimpleWorkOrderDeviceVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SimpleWorkOrderDeviceVO> pageList4app(Page page, Map<String, Object> params) {
        List<SimpleWorkOrderDeviceVO> list = baseMapper.list4app(page, params);
        if(CollectionUtils.isEmpty(list)){
            String simpleOrderProcessId = String.valueOf(params.get("simpleOrderProcessId"));
            JsonResult<List<ProcessDeviceVO>> jsonResult = enterpriseClient.deviceVOListByProcessId(Long.parseLong(simpleOrderProcessId));
            if(jsonResult.getCode() == Result.Code.SUCCESS.getCode() && jsonResult.getData().size()>0){
                for(ProcessDeviceVO item : jsonResult.getData()){
                    SimpleWorkOrderDeviceVO processDeviceVO = new SimpleWorkOrderDeviceVO();
                    processDeviceVO.setDeviceCode(item.getDeviceCode());
                    processDeviceVO.setDeviceName(item.getDeviceName());
                    list.add(processDeviceVO);
                }
            }else{
                    JsonResult<List<Device>> deviceJsonResult = enterpriseClient.getDeviceAll();
                    if(jsonResult.getCode() == Result.Code.SUCCESS.getCode() && jsonResult.getData() != null){
                        for(Device item : deviceJsonResult.getData()){
                            SimpleWorkOrderDeviceVO processDeviceVO = new SimpleWorkOrderDeviceVO();
                            processDeviceVO.setDeviceCode(item.getDeviceCode());
                            processDeviceVO.setDeviceName(item.getName());
                            list.add(processDeviceVO);
                        }
                    }
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public SimpleWorkOrderDeviceVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<SimpleWorkOrderDeviceVO> getSimpleWorkOrderDeviceList(Long simpleOrderId) {
        return baseMapper.getSimpleWorkOrderDeviceList(simpleOrderId);
    }
}
